import 'package:flutter_hx/network/HttpGo.dart';
import 'package:flutter_hx/widgets/checkIn_dialog.dart';
import 'package:get/get.dart';

class MeCheckInLogic extends GetxController {
  ///今日是否已经签到
  final _checked = false.obs;

  // {
  // "id": 0,
  // "uid": 0
  // }
  ///本月签到日期数组
  final _monthCheckList = <int>[].obs;

  bool get checked => _checked.value;

  List<int> get monthCheckList => _monthCheckList.value;

  @override
  void onInit() {
    ///查询本月已签到日期
    getMonthSign();

    ///查询今日是否签到
    checkTodaySign();
    super.onInit();
  }

  ///签到
  sign() async {
    if (_checked.value) {
      return;
    }
    final res = await HttpGo.instance.post("/memberSign/sign");
    res.ifSuccess((data) {
      _checked.value = data;
      getMonthSign();
      Get.dialog(const CheckInDialog());
    });
    res.ifFailure((e) => e.toast());
  }

  ///查询今日是否签到
  checkTodaySign() async {
    final res = await HttpGo.instance.get("/memberSign/checkSign");
    res.ifSuccess((data) => _checked.value = data);
    res.ifFailure((e) => e.toast());
  }

  ///查询本月已签到日期
  getMonthSign() async {
    final res = await HttpGo.instance.get("/memberSign/monthSign");
    res.ifSuccess((data) {
      if (data != null) {
        _monthCheckList.value = data.map<int>((e) {
          return DateTime.parse(e["addTime"]).day;
        }).toList();
      }
    });
  }
}
